App.controller("typeManagementCtr",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	
	$scope.form = {};
	var classData = [];		//分类数据
	
	var alertModel = {};	//alert模态框

	//提示信息
	$scope.alertMessage = function(message){
		alertModel.pannel = jqueryAlert({
			content: message,
			modal: true
		});
	}
	
	//获取分类数据
	$scope.getClassData = function(){ 
		$http({
			   url:	path+'/server/userManageDepts',
			   method: 'GET'   
		}).success(function(data){
			if(data.code==0){  //成功
				classData = data.result;
				$scope.showData(classData);
				$.each();
			}else if(data.code==1){  //失败
				
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//显示数据
	$scope.showData = function(classData){ 
		var html = '';
			$.each(classData, function(index, OneObj){
				html += ('<div class="form-horizontal">'
						+'<div class="form-group">'
							+'<div class="col-sm-1 textRight"></div>'
							+'<div class="col-sm-8 son">'+OneObj.dept_name+'</div>'
							+'<div class="col-sm-3 textRight">'
								+'<i class="fa fa-pencil-square-o iconHidden'+index+'" ng-click="editClass('+OneObj.id+',\''+OneObj.dept_name+'\')"></i>'
//								+'<i class="fa fa-times" ng-click="deleteClass('+OneObj.id+')"></i>'
							+'</div>'
						+'</div>'
					+'</div>');	
			});
		
		html = $compile(html)($scope);   //angularJs代码需要动态编译
		$("#tree").html(html);
	}
	
	//添加分类
	$scope.addClass = function(parent_id){ 
		$scope.form = {};
		$scope.form.p_id = parent_id;
		$scope.classWindow.center().open();   //打开弹框
	}
	
	//创建分类
	$scope.createClass = function(parent_id){ 
		if(!$scope.form.dept_name){
			$scope.alertMessage("请输入【分类名称】！");
			return;
		}
		
		var url = '';
		if($scope.form.id){
			url = path+'/server/updateUserManageDept';
		}else{
			url = path+'/server/addUserManageDept';
		}
		
		$http({
			   url: url,
			   method: 'POST',
			   data: angular.toJson($scope.form),  
		}).success(function(data){
			if(data.code==0){
				$scope.classWindow.close();
				$scope.alertMessage("成功！");
				$scope.getClassData();	
			}else{
				$scope.alertMessage("失败！");
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//编辑分类
	$scope.editClass = function(id,name){ 
		$scope.form = {};
		$scope.form.id = id;
		$scope.form.dept_name = name;
		$scope.classWindow.center().open();   //打开弹框
	}
	
	

	$scope.getClassData();		//获取分类数据
	
});